package org.belkodevelop.controller;


import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.google.code.kaptcha.Constants;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.belkodevelop.controller.helper.AuthUtils;
import org.belkodevelop.registration.dto.RegistrationDoctorDto;
import org.belkodevelop.registration.dto.RegistrationPatientDto;
import org.belkodevelop.registration.dto.RegistrationStatusDto;
import org.belkodevelop.user.domain.User;
import org.belkodevelop.user.exception.NotFoundAdmin;
import org.belkodevelop.user.exception.UserExistsException;
import org.belkodevelop.user.service.UserService;
import org.dozer.DozerBeanMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

@Controller
public class RegistrationController {
    
    private final Log log = LogFactory.getLog(getClass());   
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private AuthUtils authUtils;

    @Autowired
    private DozerBeanMapper mapper;

    private boolean checkCaptcha(HttpServletRequest request, String remoteCaptchaCode) {
        String captchaCode = (String) request.getSession().getAttribute(Constants.KAPTCHA_SESSION_KEY);
        request.getSession().removeAttribute(Constants.KAPTCHA_SESSION_KEY);
        return captchaCode != null && remoteCaptchaCode != null && captchaCode.equalsIgnoreCase(remoteCaptchaCode);
    }

    @RequestMapping(value = "/registration-patient.json", method = RequestMethod.POST)
         @ResponseBody
         public RegistrationStatusDto registrationPatient(RegistrationPatientDto registrationPatientDto,
                                                   HttpServletRequest request,
                                                   HttpServletResponse response) {

        if (checkCaptcha(request, registrationPatientDto.getCaptchaCode())) {
            User user = mapper.map(registrationPatientDto, User.class);
            String login = user.getEmail();
            String password = user.getPassword();

            userService.registerPatient(user, registrationPatientDto.getEmailDoctor());

            authUtils.login(login, password, true, request, response);
        } else {
            RegistrationStatusDto registrationStatus = new RegistrationStatusDto();
            registrationStatus.setError(true);
            registrationStatus.setCode(registrationStatus.CAPTCHA_ERROR_CODE);
            return registrationStatus;
        }
        return new RegistrationStatusDto();

    }

    @RequestMapping(value = "/registration-doctor.json", method = RequestMethod.POST)
    @ResponseBody
    public RegistrationStatusDto registrationDoctor(RegistrationDoctorDto registrationDoctorDto,
                                              HttpServletRequest request,
                                              HttpServletResponse response) {

        if (checkCaptcha(request, registrationDoctorDto.getCaptchaCode())) {
            User user = mapper.map(registrationDoctorDto, User.class);

            userService.registerDoctor(user);

        } else {
            RegistrationStatusDto registrationStatus = new RegistrationStatusDto();
            registrationStatus.setError(true);
            registrationStatus.setCode(registrationStatus.CAPTCHA_ERROR_CODE);
            return registrationStatus;
        }
        return new RegistrationStatusDto();

    }

    @ExceptionHandler(UserExistsException.class)
    @ResponseBody
    private RegistrationStatusDto handlerUserExist() {
        RegistrationStatusDto registrationStatus = new RegistrationStatusDto();
        registrationStatus.setError(true);
        registrationStatus.setCode(registrationStatus.USER_EXIST_ERROR_CODE);
        return registrationStatus;
    }

    @ExceptionHandler(NotFoundAdmin.class)
    @ResponseBody
    private RegistrationStatusDto handlerNotFoundAdmin() {
        RegistrationStatusDto registrationStatus = new RegistrationStatusDto();
        registrationStatus.setError(true);
        registrationStatus.setCode(registrationStatus.NOT_FOUND_ADMIN_CODE);
        return registrationStatus;
    }

}
